package com.yiyuan.km.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yiyuan.common.exception.SubRecExistException;
import com.yiyuan.km.dao.IKmDao;
import com.yiyuan.km.model.KmReview;
import com.yiyuan.km.model.KmType;
import com.yiyuan.km.model.Knowledge;
import com.yiyuan.km.service.IKmService;

@Service("kmService")
public class KmServiceImpl implements IKmService {
	@Resource
	private IKmDao kmDao;

	/** **************分类操作***************************** */
	public void addType(KmType kmType) {
		// 更新上级节点hasChild为1
		if (kmType.getTypePid() != null) {
			KmType pKmType = this.kmDao.getType(kmType.getTypePid());
			if (pKmType != null && pKmType.getTypeId() != null) {
				pKmType.setHasChild("1");
				this.kmDao.updateType(pKmType);
			}
		}
		this.kmDao.addType(kmType);
	}

	public void updateType(KmType kmType) {
		this.kmDao.updateType(kmType);
	}

	public void deleteType(Integer typeId) throws SubRecExistException {
		KmType kmType = this.kmDao.getType(typeId);
		if(kmType.getHasChild().equals("1")){
			throw new SubRecExistException();
		}
		//查询是否有同级分类，如果没有则更新上级hasChild为0
		List<KmType> kmTypeList = this.kmDao.findSubTypeList(kmType.getTypePid().toString());
		if (kmTypeList == null || kmTypeList.size() <= 1) {
			KmType topType = this.kmDao.getType(kmType.getTypePid());
			topType.setHasChild("0");
			this.kmDao.updateType(topType);
		}
		this.kmDao.deleteType(kmType);
	}

	public KmType getType(Integer typeId) {
		return this.kmDao.getType(typeId);
	}

	public List<KmType> findSubTypeList(String typeId) {
		return this.kmDao.findSubTypeList(typeId);
	}

	@SuppressWarnings("unchecked")
	public void moveType(Integer sTypeId, Integer tTypeId) {
		KmType sType = this.kmDao.getType(sTypeId);
		KmType tType = this.kmDao.getType(tTypeId);

		// 更新移动分类上级是否还有其他子分类，没有则需更新hasChild状态为0
		List<KmType> typeList = this.kmDao.findSubTypeList(sType.getTypePid()
				.toString());
		if (typeList != null && typeList.size() <= 1) {
			KmType pType = this.kmDao.getType(sType.getTypePid());
			pType.setHasChild("0");
			this.kmDao.updateType(pType);
		}
		// 更新源分类的父分类ID
		sType.setTypePid(tTypeId);
		this.kmDao.updateType(sType);

		// 更新目标文件夹，将子节点设为1
		tType.setHasChild("1");
		this.kmDao.updateType(tType);

	}

	/** **************知识操作****************************** */
	public void addKm(Knowledge knowledge) {
		this.kmDao.addKm(knowledge);
	}

	public void updateKm(Knowledge knowledge) {
		this.kmDao.updateKm(knowledge);
	}

	public void deleteKm(Knowledge knowledge) {
		this.kmDao.deleteKm(knowledge);
	}

	public Knowledge getKm(Integer kmId) {
		return this.kmDao.getKm(kmId);
	}

	public List<Knowledge> findKmList(int startIndex, int endIndex) {
		return this.kmDao.findKmList(startIndex, endIndex);
	}

	public int getKmPageCount() {
		return this.kmDao.getKmPageCount();
	}

	/** **************知识评论操作****************************** */
	public void addReview(KmReview kmReview) {
		this.kmDao.addReview(kmReview);
	}

	public void deleteReview(KmReview kmReview) {
		this.kmDao.deleteReview(kmReview);
	}

	public void updateReview(KmReview kmReview) {
		this.kmDao.updateReview(kmReview);
	}

	public List<KmReview> findReviewList(int startIndex, int endIndex,
			Integer kmId) {
		return this.kmDao.findReviewList(startIndex, endIndex, kmId);
	}

	public int getReviewPageCount(Integer kmId) {
		return this.kmDao.getReviewPageCount(kmId);
	}

}
